package base.map;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

public class MapTest implements Runnable{
    private ExecutorService executorService;
    private Map map;
    private AtomicInteger c;

    public MapTest(ExecutorService executorService, Map map, AtomicInteger c) {
        this.executorService = executorService;
        this.map = map;
        this.c = c;
    }

    public static void main(String[] args) {
        /*ExecutorService executorService = Executors.newSingleThreadExecutor();
        HashMap<String, Object> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 2);
        executorService.execute(new MapTest(executorService,map, new AtomicInteger(0)));*/

        Map<String, Object> map = new HashMap<>(12);
        map.put("1", 1);
        System.out.println(map.size());
    }

    @Override
    public void run() {
        try {
            System.out.println(map.toString());
            map.put("c", 3);
            map.put("d", 4);
            int i = 1/0;
            System.out.println(map.toString());
        } catch (Exception e) {
            System.out.println(map.toString());
            c.getAndIncrement();
            if (c.get()<=2) {
                executorService.execute(this);
            }
        }
    }
}
